/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { Configuration } from '../config/Configuration'
import { LoggerContext } from '../slf4j/LoggerContext';
import { Filter } from '../config/Filter'
import { Level } from '../Level';
import { Encoder } from '../encoder/Encoder';
import { FilterEnum } from '../FilterEnum'
import { Match } from '../config/Match'
import { RdbModel } from '../rdb/RdbModel';
import { TABLE_NAME, SQL_CREATE_TABLE, COLUMNS } from '../rdb/RdbConst';
import { LogBackEntity } from '../rdb/LogBackEntity';
import { Appender } from '../Appender'

export class DBAppender {
    private effectiveLevelInt: number
    private pattern: string
    private className: string
    private filter: Filter
    private maxHistory: number
    private fileNamePattern: string
    private rdb: RdbModel

    createRdb(configure: Configuration) {
        for (var i = 0; i < configure.appender.length; i++) {
            if (configure.appender[i].className == Appender.DB) {
                this.rdb = new RdbModel(TABLE_NAME, SQL_CREATE_TABLE, COLUMNS)
                this.rdb.getRdbStore(function () {
                })
            }
        }
    }

    start(loggerContext: LoggerContext, configure: Configuration, level: Level, msg: string) {

        this.rdb = new RdbModel(TABLE_NAME, SQL_CREATE_TABLE, COLUMNS)
        for (var i = 0; i < configure.logger.length; i++) {
            if (loggerContext.getLoggerName() == configure.logger[i].name) {
                this.effectiveLevelInt = configure.logger[i].level.getLevelInt()
                this.pattern = configure.appender[i].encoder.pattern.msg
                this.className = configure.appender[i].className
                if (configure.appender[i].filter != null) {
                    this.filter = configure.appender[i].filter
                    this.effectiveLevelInt = configure.appender[i].filter.level.getLevelInt()
                }
            }
        }
        if (this.effectiveLevelInt > level.getLevelInt()) {
            return
        }

        if (this.filter != null) {
            if (this.filter.className == FilterEnum.LEVEL) {
                if (this.filter.onMatch == Match.ACCEPT && this.filter.onMismatch == Match.DENY) {
                    if (level.getLevelStr() == this.filter.level.getLevelStr()) {
                        let entity = new LogBackEntity()
                        entity.level = level.getLevelStr()
                        entity.msg = Encoder.encoders(loggerContext, this.pattern, level.getLevelStr(), msg)
                        this.rdb.insertData(entity, (id) => {

                        })
                    }
                    return
                }
            } else if (this.filter.className == FilterEnum.THRESHOLD) {
                let entity = new LogBackEntity()
                entity.level = level.getLevelStr()
                entity.msg = Encoder.encoders(loggerContext, this.pattern, level.getLevelStr(), msg)
                this.rdb.insertData(entity, (id) => {

                })
            }
        } else {
            let entity = new LogBackEntity()
            entity.level = level.getLevelStr()
            entity.msg = Encoder.encoders(loggerContext, this.pattern, level.getLevelStr(), msg)
            this.rdb.insertData(entity, (id) => {

            })
        }
    }
}